/*
 * @lc app=leetcode.cn id=1769 lang=golang
 *
 * [1769] 移动所有球到每个盒子所需的最小操作数
 */
package Solutions

// @lc code=start
func minOperations(boxes string) []int {
	leftMove := 0
	rightMove := 0
	rightNum := 0
	leftNum := 0
	for i := len(boxes) - 1; i >= 0; i-- {
		if boxes[i] == '1' {
			rightMove += i + 1
			rightNum += 1
		}
	}
	var ans = make([]int, len(boxes))
	for i, v := range boxes {
		rightMove -= rightNum
		if v == '1' {
			rightNum -= 1
		}
		ans[i] = leftMove + rightMove
		if v == '1' {
			leftNum += 1

		}
		leftMove += leftNum
	}
	return ans
}

// @lc code=end
